package com.xidian.zhiancloud.service.dangerWorkTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.WorkLedgerMapper;
import com.xidian.zhiancloud.dao.syset.SysPositionMapper;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketTemplateMapper;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicketTemplate;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class WorkLedgerService extends ServiceImpl<WorkLedgerMapper, WorkTicket> implements IService<WorkTicket> {
    @Autowired
    private WorkLedgerMapper workLedgerMapper;

    @Autowired
    private WorkTicketTemplateMapper workTicketTemplateMapper;

    @Autowired
    private SysPositionMapper sysPositionMapper;

    public IPage<WorkTicketVO> getListByQO(WorkTicketQO workTicketQO){                // 分页
        Page<WorkTicketVO> page = new Page<>(workTicketQO.getCurrent(), workTicketQO.getSize());
        OrderItem orderItem = new OrderItem();
        // 排序
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<WorkTicketVO> res = workLedgerMapper.getListByQO(page, workTicketQO);
        // 联表查询
        res.getRecords().forEach(
                vo -> {
                    Long ticketType = vo.getTicketType();
                    WorkTicketTemplate template = workTicketTemplateMapper.selectById(ticketType);
                    vo.setWorkTemplateName(
                            template == null ? "作业票类型已删除" : template.getName()
                    );
                    SysPosition position = sysPositionMapper.selectById(vo.getPositionId());
                    vo.setLocationName(position == null ? "该位置不存在" : position.getName());

                }
        );
        return res;
    }
}
